package main.java.io;


import java.io.IOException;
import java.net.InetSocketAddress;

import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;


public class io3 {
    public static void main(String[] args) throws IOException, InterruptedException {
        int port = 8888;
        ByteBuffer bf = ByteBuffer.wrap("hello".getBytes());

        ServerSocketChannel ssc = ServerSocketChannel.open();
        ssc.socket().bind(new InetSocketAddress(port));

        //设置非阻塞
        ssc.configureBlocking(false);

        //监听新链接传入
        while (true){
            SocketChannel sc = ssc.accept();
            if(sc == null){
                Thread.sleep(2000);
            }else {
                System.out.println(sc.socket().getRemoteSocketAddress());
                bf.rewind();
                sc.write(bf);
                sc.close();
            }

        }



    }
}
